package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.*;
import com.zhentao.domain.vo.Result;
import com.zhentao.mapper.SysActivityMapper;
import com.zhentao.service.*;
import com.zhentao.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
* @author 86133
* @description 针对表【sys_activity】的数据库操作Service实现
* @createDate 2024-08-20 17:17:50
*/
@Service
public class SysActivityServiceImpl extends ServiceImpl<SysActivityMapper, SysActivity>
    implements SysActivityService{
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private SysUserPreferentialService sysUserPreferentialService;
    @Autowired
    private SysPreferentialService sysPreferentialService;
    @Autowired
    private SysPreferentialActivityService sysPreferentialActivityService;
    @Autowired
    private SysActivityMapper sysActivityMapper;
    @Autowired
    private SysActivityRecordService sysActivityRecordService;
    @Autowired
    private SysUserService sysUserService;
//查询活动
    @Override
    public List<SysActivity> activityList() {
        List<SysActivity> sysActivities = sysActivityMapper.activityList();
        sysActivities.forEach(sysActivity -> {
            Date endTime = sysActivity.getEndTime();
            LocalDate today = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate end = endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long daysBetween = ChronoUnit.DAYS.between(today, end);
            if(daysBetween < 0){
                sysActivity.setStatus(1);
                this.updateById(sysActivity);
            System.out.println("活动已经过去了");
            }else {
                System.out.println("活动还有"+daysBetween+"天");
            }
        });
        return sysActivities;
    }
    //    点击领取优惠卷数量减一
    @Override
    public Result updateAcnum(SysActivity sysActivity) {
        SysActivity byId = this.getById(sysActivity.getId());

//        SysUserPreferential existingPreferential = sysUserPreferentialService.getByUserIdAndPreferentialId(userUtils.GetUserId(), sysActivity.getId());
//        if (existingPreferential != null) {
//            return Result.build(null, 203, "每个用户只能领取一个优惠券");
//        }

        if (byId.getAcnum()>0){
            sysActivity.setAcnum(sysActivity.getAcnum()-1);
            this.updateById(sysActivity);

            // 尝试获取现有的优惠券记
                SysPreferential sysPreferential = new SysPreferential();
                    sysPreferential.setPreferentialName(sysActivity.getActivityName());
                    sysPreferential.setRemark("活动获取");
                    sysPreferential.setStartrTime(sysActivity.getStartTime());
                    sysPreferential.setEndTime(sysActivity.getEndTime());
                    sysPreferential.setLssueNum(sysActivity.getAcnum());
                    sysPreferential.setPerferentialMoney(sysActivity.getActicityMony());
                    sysPreferentialService.save(sysPreferential);
                    if(sysPreferentialService.getByName(sysActivity.getActivityName())!=null) {
                return Result.build(null,203,"一个用户只能领取一个优惠卷");
            }
//                用户表和优惠卷中间表
                SysUserPreferential sysUserPreferential = new SysUserPreferential();
                sysUserPreferential.setUserId(userUtils.GetUserId());
                sysUserPreferential.setPreferentialId(sysPreferential.getId());
                sysUserPreferential.setNum(sysActivity.getAcnum());
                sysUserPreferentialService.save(sysUserPreferential);
//               优惠卷和活动表
            SysPreferentialActivity sysPreferentialActivity = new SysPreferentialActivity();
            sysPreferentialActivity.setActivityId(sysActivity.getId());
            sysPreferentialActivity.setPreferentialId(sysPreferential.getId());
            sysPreferentialActivityService.save(sysPreferentialActivity);

//            活动领取记录
           SysActivityRecord sysActivityRecord = new SysActivityRecord();
            int i = userUtils.GetUserId();
            SysUser byId1 = sysUserService.getById(i);
            sysActivityRecord.setRecordName(byId1.getNickname());
            sysActivityRecord.setActivityTitle(sysActivity.getActicityTitle());
            sysActivityRecord.setActivityName(sysActivity.getActivityName());
           sysActivityRecord.setActivityRemark(sysActivity.getRemark());
            sysActivityRecord.setActivityImgs(sysActivity.getImgs());
            sysActivityRecord.setActivityMony(sysActivity.getActicityMony());
            sysActivityRecord.setRecordData(new Date());
            sysActivityRecordService.save(sysActivityRecord);
            return Result.build(null,200, "领取成功");
        }else {
            return Result.build(null,202, "领取失败,活动已被领取完了");
        }
    }
//添加活动
    @Override
    public Result addActivity(SysActivity sysActivity) {
        this.save(sysActivity);
        return Result.build(null,200,"添加成功");
    }
}




